package com.imay.live.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by  nick on 2017/4/18 0018.
 */

public class TimeUtil {
    public final static long MINUTE = 60 * 1000;// 1分钟
    public final static long HOUR = 60 * MINUTE;// 1小时
    public final static long DAY = 24 * HOUR;// 1天

    /**
     * @param time
     * @return
     */
    public static String getFriendlyTime(long time) {

        Date date = new Date(time);

        if (date == null) {
            return "";
        }
        long diff = new Date().getTime() - date.getTime() * 1000;
        long r = 0;
        if (diff > DAY) {
            r = (diff / DAY);
            return r + "天前";
        }
        if (diff > HOUR) {
            r = (diff / HOUR);
            return r + "小时前";
        }
        if (diff > MINUTE) {
            r = (diff / MINUTE);
            return r + "分钟前";
        }
        return "刚刚";
    }

    /**
     * @param time
     * @return
     */
    public static String getFormatTime(long time) {

        Date date = new Date(time * 1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr = format.format(date);
        return timeStr;
    }



    /**
     * @param time
     * @return
     */
    public  static String getTodayTime(long time) {

        Date date = new Date(time*1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String timeStr = format.format(date);
        return timeStr;
    }

    //判断选择的日期是否是本周
    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time*1000));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    //判断选择的日期是否是今天
    public static boolean isToday(long time) {
        return isThisTime(time*1000, "yyyy-MM-dd");
    }

    private static boolean isThisTime(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param time
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(long time) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time*1000));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 返回时分
     * @param time
     * @return
     */
    public  static String getCallLiveTime(long time) {

        int hours = (int) (time/3600);
        int minute = (int) ((time-hours*3600)/60 );
        return hours+":"+minute;
    }

    /**
     * * 返回当前时间年月日
     * @return
     */
    public static String getToday() {
        Date date = new Date();
        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = format.format(date);
        return timeStr;
    }

}
